const initSwiper = ($this, arr, callback) => {
	$this.swiperList = arr.length > 3 ? [arr[0], arr[1], arr[2]] : arr;
	if (callback) {
		callback()
	}
};
const swiperCompute = ($current, $currItemNo, currIdx, arr, callback) => {
	if ($current == currIdx) {
		return null
	};
	var max = arr.length;
	let currItemNo = $currItemNo;
	let swiperList;
	let current;
	if (max > 3 && (currItemNo == 1 && currIdx == 1 || currItemNo > 1 && currIdx == 2 && currItemNo < max)) {
		currItemNo += 1;
		swiperList = max <= 3 ? arr : (currItemNo == 2 ? [arr[currItemNo - 2], arr[currItemNo - 1], arr[
			currItemNo]] : (currItemNo == max ? [arr[currItemNo - 3], arr[currItemNo - 2], arr[currItemNo -
				1]] : [arr[currItemNo - 2], arr[currItemNo - 1], arr[currItemNo]]));
		current = currItemNo == 2 && currIdx == 1 ? 1 : (currItemNo < max ? 1 : 2);
		if (callback) {
			callback({
				current,
				swiperList,
				currItemNo
			})
		}
	} else if (max > 3 && (currIdx == 0 && currItemNo > 1 || currIdx == 1 && currItemNo == max)) {
		currItemNo = currItemNo - 1;
		swiperList = max <= 3 ? arr : (currItemNo > 2 ? [arr[currItemNo - 2], arr[currItemNo - 1], arr[
			currItemNo]] : [arr[0], arr[1], arr[2]]);
		current = currItemNo == 1 ? 0 : 1;
		if (callback) {
			callback({
				current,
				swiperList,
				currItemNo
			})
		}
	} else if (max <= 3) {
		current = currIdx;
		currItemNo = currIdx + 1;
		if (callback) {
			callback({
				current,
				swiperList: arr,
				currItemNo
			})
		}
	}
};
const swiperLeapCompute = (num, $currItemNo, arr, callback) => {
	if ($currItemNo != num) {
		const idx = num - 1;
		let swiperList, current;
		if (idx == 0) {
			swiperList = arr.length <= 3 ? arr : [arr[idx], arr[idx + 1], arr[idx + 2]];
			current = 0
		} else if (idx == arr.length - 1) {
			swiperList = arr.length <= 3 ? arr : [arr[idx - 2], arr[idx - 1], arr[idx]];
			current = 2
		} else {
			swiperList = arr.length <= 3 ? arr : [arr[idx - 1], arr[idx], arr[idx + 1]];
			current = 1
		};
		if (callback) {
			callback({
				current,
				swiperList: arr,
				currItemNo: num
			})
		}
	}
};
const leapToItem = ($this, num, list, duration, callback) => {
	const $ = $this;
	if ($.currItemNo == num) {
		if (callback) {
			callback()
		}
	} else {
		const idx = num - 1;
		let swiperList, current;
		if (idx == 0) {
			swiperList = list.length <= 3 ? list : [list[idx], list[idx + 1], list[idx + 2]];
			current = 0
		} else if (idx == list.length - 1) {
			swiperList = list.length <= 3 ? list : [list[idx - 2], list[idx - 1], list[idx]];
			current = 2
		} else {
			swiperList = list.length <= 3 ? list : [list[idx - 1], list[idx], list[idx + 1]];
			current = 1
		};
		// 使用更平滑的切换方式，避免duration为0导致的闪烁
		const originalDuration = $.duration;
		$.duration = 0;
		$.$nextTick(() => {
			$.swiperList = [...swiperList];
			$.current = current;
			$.currItemNo = num;
			$.$nextTick(() => {
				// 延迟恢复duration，确保DOM更新完成
				setTimeout(() => {
					$.duration = duration;
					if (callback) {
						callback()
					}
				}, 50)
			})
		})
	}
};
const changeSwiperItem = ($this, currIdx, list, duration, callback) => {
	const $ = $this;
	if ($.current == currIdx) {
		return
	}
	var arr = list;
	var max = arr.length;
	let currItemNo = $.currItemNo;
	let swiperList;
	let current;
	if (max > 3 && (currItemNo == 1 && currIdx == 1 || currItemNo > 1 && currIdx == 2 && currItemNo < max)) {
		currItemNo += 1;
		swiperList = max <= 3 ? arr : (currItemNo == 2 ? [arr[currItemNo - 2], arr[currItemNo - 1], arr[
			currItemNo]] : (currItemNo == max ? [arr[currItemNo - 3], arr[currItemNo - 2], arr[currItemNo -
				1]] : [arr[currItemNo - 2], arr[currItemNo - 1], arr[currItemNo]]));
		current = currItemNo == 2 && currIdx == 1 ? 1 : (currItemNo < max ? 1 : 2);
		$.current = currIdx;
		// 使用更平滑的切换方式，避免duration为0导致的闪烁
		const originalDuration = $.duration;
		$.duration = 0;
		$.$nextTick(() => {
			$.swiperList = [...swiperList];
			$.current = current;
			$.currItemNo = currItemNo;
			$.$nextTick(() => {
				// 延迟恢复duration，确保DOM更新完成
				setTimeout(() => {
					$.duration = duration;
					if (callback) {
						callback()
					}
				}, 50)
			})
		})
	} else if (max > 3 && (currIdx == 0 && currItemNo > 1 || currIdx == 1 && currItemNo == max)) {
		currItemNo = currItemNo - 1;
		swiperList = max <= 3 ? arr : (currItemNo > 2 ? [arr[currItemNo - 2], arr[currItemNo - 1], arr[
			currItemNo]] : [arr[0], arr[1], arr[2]]);
		current = currItemNo == 1 ? 0 : 1;
		$.current = currIdx;
		// 使用更平滑的切换方式，避免duration为0导致的闪烁
		const originalDuration2 = $.duration;
		$.duration = 0;
		$.$nextTick(() => {
			$.swiperList = [...swiperList];
			$.current = current;
			$.currItemNo = currItemNo;
			$.$nextTick(() => {
				// 延迟恢复duration，确保DOM更新完成
				setTimeout(() => {
					$.duration = duration;
					if (callback) {
						callback()
					}
				}, 50)
			})
		})
	} else if (max <= 3) {
		$.current = currIdx;
		// 修复：确保currItemNo不超过数组长度
		$.currItemNo = Math.min(currIdx + 1, max);
		if (callback) {
			callback()
		}
	}
};

const defaultParams = {
	list : [],
	duration: 400
};

export {
	initSwiper,
	swiperCompute,
	swiperLeapCompute,
	leapToItem,
	changeSwiperItem,
	defaultParams
}
